﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.utils.Timer;
		
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;

	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	import com.adobe.ep.notifications.NotificationManager;
	import com.adobe.ep.notifications.NotificationEvent;
	import com.adobe.ep.notifications.Notification;
	import com.adobe.ep.notifications.NotificationAlertPolicy;
	import com.adobe.ep.notifications.NotificationEvent;
	import com.adobe.ep.notifications.NotificationIconType;
	
	public class TestANE extends MovieClip {
		public var notificationManager:NotificationManager = new NotificationManager();
		public var n:Notification;
		
		// You can set this value to anything you want (and use various values)
		// You can then pass this value into the notification manager to cancel notifications of a specified type
		public const NOTIFICATION_CODE:String = "MY_CUSTOM_NOTIFICATION";
		
		public var timedNotification:Timer;
		
		public function TestANE() {
			// constructor code
			//[SWF(height="800", width="480", frameRate="20", backgroundColor="#996666")];
			//stage.align = StageAlign.TOP_LEFT;
			//stage.scaleMode = StageScaleMode.NO_SCALE;
			if ( notificationManager.isSupported )
				createManager();
			else
			    trace("The notification native extension is not supported.");
		}
		
		//setup notification manager
		protected function createManager():void
		{
				//notificationManager = new NotificationManager();
				
				n = new Notification();
		   
				// This action label appears in two places on iOS: on the action button of the notification dialog, and on the unlock slider when the device is locked.
				n.actionLabel = "Open";// If unset, the iOS default will be used.
				
				// Allows you to contorl whether an alert is dispathed with each notification, or just the first notification.
				n.alertPolicy = NotificationAlertPolicy.EACH_NOTIFICATION;
				
				// On Android, specifies whether the notification persists when the user taps it in the notification area
				n.cancelOnSelect = true;
				
				// On Android and iOS "...the app will be brought to the foreground if it was in the background or launched if it had been shutdown.
				// On iOS, the way to perform the action of a notification manifests itself as a button 
				// on the notification dialog that appears when a notification is fired and different
				// text on the unlock slider when the device is locked. 
				// On Android, the way to perform an action is not visible, it is performed by selecting 
				// the notification from the notification list (window shade)."
				n.hasAction = true;
				n.actionData = "customAction";
				
				// Allows you to set the Android notification icon
				n.iconType = NotificationIconType.ALERT;
				
				// On both Android and iOS, lets you set a number on the icon or application badge
				n.numberAnnotation = 0;
								
				// On Android, "ongoing" notifications aren't cleared with the clear button
				n.ongoing = false;
								
				// Android and iOS
				n.playSound = true;
				
				// On Android, alerts (sound and vibration) until acknowledged 
				n.repeatAlertUntilAcknowledged = true;
				
				// Only configurable on Android
				n.vibrate = true;
					
				n.tickerText = "TestANE Ticker Text";
				n.title = "TestANE";
				n.body = "Alarm Sent!";  
				
				//user touches notification
				this.notificationManager.addEventListener(NotificationEvent.NOTIFICATION_ACTION,onNotificationActionEvent);
				this.notificationManager.addEventListener(StatusEvent.STATUS,onNotificationStatusEvent);
				
				//start timer to send notification
				startTimer();
				
				//setup buttons
				
				/** 임시삭제 */
				// notify_btn.addEventListener(MouseEvent.CLICK, notify_ClickHandler);
				// cancel_btn.addEventListener(MouseEvent.CLICK, cancelNotification_clickHandler);
		}
		
		//send notification
		protected function notifyUser():void
		{	
			// Dispatch the notification
			// Note that the way popups work on iOS dictate that a pop-up window will only appear when the application is in the background. 
			// If the notification is dispatched and the app is running normally, you won't see a pop-up (but you will receive the event).
			this.notificationManager.notifyUser(NOTIFICATION_CODE,n);
			trace("air notification sent");   
		}
		
		//notify button
		public function notify_ClickHandler(event:MouseEvent):void{
			notifyUser();
		}
		
		//cancel button
		protected function cancelNotification_clickHandler(event:MouseEvent):void
		{
			if ( this.notificationManager )
			{					
				this.notificationManager.cancel(NOTIFICATION_CODE);
				trace("Notification canceled.");
			}				
			else
			{
				trace("No notifications have been dispatched.");					
			}
		}		
		
		//user touches notification
		private function onNotificationActionEvent(ne:NotificationEvent):void
		{            
			trace("Notification action received. Type: " + ne.actionData);            
		}
		
		private function onNotificationStatusEvent(ne:StatusEvent):void
		{
			trace("Notification status event received!");
		}
			
		//start timer notification
		protected function startTimer():void{
			timedNotification = new Timer(5000,1)
			timedNotification.start();
			timedNotification.addEventListener(TimerEvent.TIMER, callNotifyUser);
		}
		
		//timer event
		protected function callNotifyUser(event:TimerEvent):void{
			notifyUser();
			timedNotification.removeEventListener(TimerEvent.TIMER, callNotifyUser);
			timedNotification.stop();
		}
	}
}